package org.dromara.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.business.domain.ContentComment;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.ContentCommentVo;
import org.dromara.business.domain.bo.ContentCommentBo;
import org.dromara.business.service.IContentCommentService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 统一内容评论
 *
 * @author Hao Tong Xue
 * @date 2025-04-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/contentComment")
public class ContentCommentController extends BaseController {

    private final IContentCommentService contentCommentService;

    /**
     * 查询统一内容评论列表
     */
//    @SaCheckPermission("business:contentComment:list")
    @GetMapping("/list")
    public TableDataInfo<ContentCommentVo> list(ContentCommentBo bo, PageQuery pageQuery) {
        return contentCommentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出统一内容评论列表
     */
    @SaCheckPermission("business:contentComment:export")
    @Log(title = "统一内容评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ContentCommentBo bo, HttpServletResponse response) {
        List<ContentCommentVo> list = contentCommentService.queryList(bo);
        ExcelUtil.exportExcel(list, "统一内容评论", ContentCommentVo.class, response);
    }

    /**
     * 获取统一内容评论详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:contentComment:query")
    @GetMapping("/{id}")
    public R<ContentCommentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(contentCommentService.queryById(id));
    }

    /**
     * 新增统一内容评论
     */
//    @SaCheckPermission("business:contentComment:add")
    @Log(title = "统一内容评论", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ContentCommentBo bo) {
        return toAjax(contentCommentService.insertByBo(bo));
    }

    /**
     * 修改统一内容评论
     */
    @SaCheckPermission("business:contentComment:edit")
    @Log(title = "统一内容评论", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ContentCommentBo bo) {
        return toAjax(contentCommentService.updateByBo(bo));
    }

    /**
     * 删除统一内容评论
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:contentComment:remove")
    @Log(title = "统一内容评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(contentCommentService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 点赞或取消
     * @param bo data 0 点赞 1 取消点赞
     * @return void
     */
    @PostMapping("/likeCount")
    public R<Void> likeCount(@RequestBody ContentCommentBo bo) {
        contentCommentService.likeCount(bo);
        return R.ok();
    }
}
